package com.mybatis.mapper;

import basic.framework.components.mybatis.executor.dto.ColumnDto;
import com.mybatis.dto.MyColumnDto;
import com.mybatis.dto.MyEntityDto;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

public class MyCrudProvider {
    public static String insertSql(MyEntityDto dto)
    {
        SQL sql = new SQL().INSERT_INTO(dto.getTableName());
        //map循环就用了for each了，就是用什么iterator
        for (Map.Entry<String,MyColumnDto> entry : dto.getMap().entrySet())
        {
            //此处不判段是否不可增加了，不知道干嘛的,还有为什么要判断是不是主键，感觉没必要啊
            //照着写一遍吧
            if (null!=dto.getMyPrimaryKeyDto()&&
                    !dto.getMyPrimaryKeyDto().getPropertyName().equals(entry.getKey()))
            {
                //处理正常字段，不是主键的字段,事实证明不需要判断， 除非业务需要
                sql.VALUES(entry.getValue().getColumnName(),"<if test=\"" + entry.getKey() + " != null\">#{"
                        + entry.getKey() + "}</if>" + "<if test=\"" + entry.getKey() + " == null\">null</if>");
            }
            else {
                sql.VALUES(entry.getValue().getColumnName(), "#{" + entry.getKey() + "}");
            }
        }
        return sql.toString();
    }

    public static String updateSql(MyEntityDto myEntityDto,boolean isWhole) {
        StringBuilder colString = new StringBuilder();
        for (Map.Entry<String,MyColumnDto> entry:myEntityDto.getMap().entrySet())
        {
            String key = entry.getKey();
            String column = entry.getValue().getColumnName();
            //判断是否是主键属性，主键属性不允许修改i
            if (!key.equals(myEntityDto.getMyPrimaryKeyDto().getPropertyName()))
            {
                if (isWhole)
                {
                    colString.append(column).append(" = #{").append(key).append("},");
                }
                else
                {
                    colString.append("<if test=\"").append(entry.getKey()).append(" != null\">")
                            .append(column).append(" = #{").append(key).append("},").append("</if>");
                }
            }
        }
        String sql = "update  " + myEntityDto.getTableName() + " <set> " + colString.toString()
                + " </set> where " + myEntityDto.getMyPrimaryKeyDto().getCloumnName()
                + "= #{" + myEntityDto.getMyPrimaryKeyDto().getPropertyName() + "}";
        return sql;
    }

    public static String getByIdSql(MyEntityDto myEntityDto) {
        //项目组还判断字段是否ignore，lazy以及默认值等,还有as后面双引号不知道干嘛的，别名？
        StringBuilder colString = new StringBuilder();
        for (Map.Entry<String,MyColumnDto> entry: myEntityDto.getMap().entrySet())
        {
            String key = entry.getKey();
            String column = entry.getValue().getColumnName();
            if (colString.length()==0)
            {
                colString.append(column).append(" as ").append(key);
            }
            else {
                colString.append(", ").append(column).append(" as ").append(key);
            }
        }
        String sql = "select " + colString.toString() + " from "+ myEntityDto.getTableName()+ " where "
                + myEntityDto.getMyPrimaryKeyDto().getCloumnName() + " = #{"
                +myEntityDto.getMyPrimaryKeyDto().getPropertyName()+"}";
        return sql;
    }
    public static String insertBatchSql(MyEntityDto myEntityDto){

        SQL sql = new SQL().INSERT_INTO(myEntityDto.getTableName());
        StringBuilder colStr = new StringBuilder();
        StringBuilder dataStr = new StringBuilder();
        for (Map.Entry<String,MyColumnDto> entry:myEntityDto.getMap().entrySet())
        {
            if (colStr.length()==0)
            {
                colStr.append(entry.getValue().getColumnName());
            }
            else {
                colStr.append(",").append(entry.getValue().getColumnName());
            }
            if (dataStr.length()==0)
            {
                dataStr.append("#{item.").append(entry.getKey()).append("}");
            }
            else {
                dataStr.append(",").append("#{item.").append(entry.getKey()).append("}");
            }
        }
        String valueString = "VALUES <foreach item=\"item\" collection=\"list\" separator=\",\">("
                +dataStr.toString()+")</foreach>"; //这里的括号也可以直接用open和close两个属性
        return sql.toString() + "(" + colStr.toString() + " )" + valueString;
    }

    public static String deleteBatchSql(MyEntityDto myEntityDto) {
        String sql = "delete from " + myEntityDto.getTableName() +
                " where " +myEntityDto.getMyPrimaryKeyDto().getCloumnName()+ " in"
                + " <foreach item=\"item\" collection=\"list\" separator=\",\" open=\"(\" close=\")\">"
                + "#{item} </foreach>";
        return sql;
    }

    public static String getListSql(MyEntityDto myEntityDto) {
        StringBuilder colStr = new StringBuilder();
        for (Map.Entry<String, MyColumnDto> entry : myEntityDto.getMap().entrySet()) {
            String key = entry.getKey();
            String column = entry.getValue().getColumnName();
            if (colStr.length()==0)
            {
                colStr.append(column).append(" as ").append(key);
            }
            else {
                colStr.append(", ").append(column).append(" as ").append(key);
            }

        }
        return "select " + colStr.toString() + " from " + myEntityDto.getTableName() + " where 1=1 ";
    }
}
